package com.huizhonglm.diap.widget.shortcuts;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.model.Shortcut;

import java.util.List;

/**
 * GridPager Item 填充器
 * 
 * @author LiXiaolong
 * @since 2014年9月24日下午1:13:01
 * @version 1.0
 */
public class ShortcutsGridAdapter extends BaseAdapter {

	private List<Shortcut> data;

	private LayoutInflater inflater;

	private int size = 0;

	public ShortcutsGridAdapter(Context context, List<Shortcut> list) {
		this.inflater = LayoutInflater.from(context);
		this.data = list;
		this.size = list.size();
	}

	@Override
	public int getCount() {
		return this.size;
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.item_shortcuts_grid, null);
			holder.icon = (ImageView) convertView.findViewById(R.id.grid_pager_item_img);
			holder.name = (TextView) convertView.findViewById(R.id.grid_pager_name_txt);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Shortcut item = data.get(position);
		if (item.getDrawable() > 0){
			holder.icon.setVisibility(View.VISIBLE);
			holder.name.setVisibility(View.VISIBLE);
			holder.icon.setTag(item);
			holder.icon.setImageResource(item.getDrawable());
			holder.name.setText(item.getName());
		} else {
			holder.icon.setVisibility(View.INVISIBLE);
			holder.name.setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	class ViewHolder {

		public ImageView icon;
		public TextView name;
	}
}